// 引用封装好的数据库连接对象
const mongoose = require('../config/db')
// 定义模型（Schema）
const UserSchema = new mongoose.Schema({
  // roles: [{ type: Schema.Types.ObjectId, ref: 'Role' }], // 关联角色
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  roles: {
    type: Array,
    default: ['admin']
  },
  token: String,
  introduction: {
    type: String,
    default: 'I am a super administrator'
  },
  avatar: {
    type: String,
    default:
      'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
  },
  createTime: {
    type: Date,
    default: Date.now
  },
  lastLoginTime: {
    type: Date,
    default: Date.now
  },
  status: {
    type: Number,
    default: 1
  }
})

// 在username字段上创建唯一索引
UserSchema.index({ username: 1 }, { unique: true })

const User = mongoose.model('User', UserSchema)

module.exports = User
